<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Myrole extends BaseController
{
    public function show_list(){
        return Builder::table()
            ->setAction('myrole/page?model_id=12')
            ->setMonitorAction('switch','myrole/change')
            ->setRightHeader()
            ->addToolbarDemo('alldel','全部删除',[],'myrole/del')
            ->addToolbarDemo('add','添加',[],'myrole/show_add')
            ->setButton('detail','详情','myrole/detail')
            ->setButton('edit','编辑','myrole/show_edit')
            ->setButton('add','添加','myrole/show_add')
            ->setButton('del','删除','myrole/del')
            ->setColumn('input','name','角色名','myrole/change')
            ->setColumn('input','pid','上级','myrole/change')
            ->setColumn('img','iocn','图标','myrole/change')
            ->setColumn('switch','status','状态','myrole/change')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit');
        $data = Db::name(getTable('roles'))
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['pid']==0){
                $value['pid'] = '最上级';
            }else{
                $role = Db::name(getTable('roles'))
                    ->where('id',$value['pid'])
                    ->find();
                if($role){
                    $value['pid'] = $role['name'];
                }else{
                    $value['pid'] = '';
                }
            }
            $value['iocn'] = getFilePath($value['iocn']);
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function show_add(Request $request){
        $id = $request->param('id',null);
        $user = session('adminuser');
        if($id){
            $role = Db::name(getTable('roles'))
                ->where('id',$id)
                ->find();
        }else{
            $role = Db::name(getTable('roles'))
                ->where('id',$user['role_id'])
                ->find();
        }

        $array = Db::name(getTable('jurs'))
            ->where('id','in',$role['role'])
            ->where('status',1)
            ->order('id desc')
            ->field('id,name')
            ->select()->toArray();
        $array = $this->getGroupu($array);
        foreach ($array as $key=>$value){
            $value['title'] = $value['name'];
            $value['spread'] = true;
            $child = Db::name(getTable('jurs'))
                ->where('status',1)
                ->where('name',$value['name'])
                ->order('id desc')
                ->field('id,descs')
                ->select()->toArray();
            foreach ($child as $k=>$v){
                $child[$k]['title'] = $v['descs'];
            }
            $value['children'] = $child;
            $array[$key] = $value;
        }
//        echo '<pre>';
//        print_r($array);
//        exit;
        return Builder::form()
            ->setAction('myrole/add')
            ->setProject('none','pid','',$id??$role['id'])
            ->setProject('input','name','角色名称','')
            ->setProject('img','iocn','图标','')
            ->setProject('switch','status','状态','')
            ->setProject('tree','role','权限','',$array)
            ->setButton('submit','提交')
            ->setButton('back')
            ->fetch();
    }
    public function add(Request $request){
        $data = $request->param();
        if($data['status']??null){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $newdata = [];
        $newdata['name'] = $data['name'];
        $newdata['pid'] = $data['pid'];
        $newdata['role'] = $data['role'];
        $newdata['iocn'] = $data['iocn'];
        $newdata['status'] = $data['status'];
        if(Db::name(getTable('roles'))->insert($newdata,true)){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('roles'))
            ->where('id',$id)
            ->find();
        if($data['pid']==0){
            $role = Db::name(getTable('roles'))
                ->where('id',1)
                ->find();
        }else{
            $role = Db::name(getTable('roles'))
                ->where('id',$data['pid'])
                ->find();
        }
        $array = Db::name(getTable('jurs'))
            ->where('id','in',$role['role'])
            ->where('status',1)
            ->field('id,name')
            ->order('id desc')
//            ->group('name')
            ->select()->toArray();
        $array = $this->getGroupu($array);
        $newrole = $data['role'];
        $newrole = explode(',',$newrole);
        $role = explode(',',$role['role']);
        foreach ($array as $key=>$value){
            $value['title'] = $value['name'];
            $value['spread'] = true;
            $child = Db::name(getTable('jurs'))
                ->where('status',1)
                ->where('name',$value['name'])
                ->field('id,descs')
                ->order('id desc')
                ->select()->toArray();
            foreach ($child as $k=>$v){
                $child[$k]['title'] = $v['descs'];
                if(in_array($v['id'],$newrole)){
                    $child[$k]['checked'] = true;
                }
            }
            $value['children'] = $child;
            $array[$key] = $value;
        }
        return Builder::form()
            ->setAction('myrole/edit')
            ->setProject('none','id','',$id)
            ->setProject('none','pid','',$id??$role['id'])
            ->setProject('input','name','角色名称',$data['name'])
            ->setProject('img','iocn','图标',$data['iocn'])
            ->setProject('switch','status','状态',$data['status'])
            ->setProject('tree','role','权限','',$array)
            ->setButton('submit')
            ->setButton('back')
            ->fetch();

    }
    public function detail(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('roles'))
            ->where('id',$id)
            ->find();
        if($data['pid']==0){
            $role = Db::name(getTable('roles'))
                ->where('id',1)
                ->find();
        }else{
            $role = Db::name(getTable('roles'))
                ->where('id',$data['pid'])
                ->find();
        }
        $array = Db::name(getTable('jurs'))
            ->where('id','in',$role['role'])
            ->where('status',1)
            ->field('id,name')
            ->order('id desc')
//            ->group('name')
            ->select()->toArray();
        $array = $this->getGroupu($array);
        $role = explode(',',$role['role']);
        $newrole = $data['role'];
        $newrole = explode(',',$newrole);
        foreach ($array as $key=>$value){
            $value['title'] = $value['name'];
            $value['spread'] = true;
            $child = Db::name(getTable('jurs'))
                ->where('status',1)
                ->where('name',$value['name'])
                ->field('id,descs')
                ->order('id desc')
                ->select()->toArray();
            foreach ($child as $k=>$v){
                $child[$k]['title'] = $v['descs'];
                if(in_array($v['id'],$newrole)){
                    $child[$k]['checked'] = true;
                }
            }
            $value['children'] = $child;
            $array[$key] = $value;
        }
        return Builder::form()
            ->setAction('myrole/edit')
            ->setProject('none','id','',$id)
            ->setProject('none','pid','',$id??$role['id'])
            ->setProject('input','name','角色名称',$data['name'])
            ->setProject('img','iocn','图标',$data['iocn'])
            ->setProject('switch','status','状态',$data['status'])
            ->setProject('tree','role','权限','',$array)
//            ->setButton('submit')
            ->setButton('back')
            ->fetch();

    }
    public function change(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('roles'))
            ->where('id',$id)
            ->find();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Db::name(getTable('roles'))->where('id',$data['id'])->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(Db::name(getTable('roles'))->where('id','in',$id)->delete()){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function getGroupu($array=[]){
        $newarray = [];
        $xinarray = [];
        foreach ($array as $value){
            if(!in_array($value['name'],$newarray)){
                $newarray[] = $value['name'];
                $xinarray[] = $value;
            }
        }
        return $xinarray;
    }
}